package com.jz.community.modulemine.utils;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;

import com.jz.community.basecomm.base.BaseApplication;
import com.orhanobut.logger.Logger;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Ani_H on 2018/2/23.
 * 检测通知是否开启工具类
 */

public class NotificationsUtil {
    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

    /**
     * 4.3以上
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static boolean isNotificationEnabled(Context context) {
        AppOpsManager mAppOps = (AppOpsManager)
                context.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (int) opPostNotificationValue.get(Integer.class);
            return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (NoSuchMethodException e) {
            e.printStackTrace();

        } catch (NoSuchFieldException e) {
            e.printStackTrace();

        } catch (InvocationTargetException e) {
            e.printStackTrace();

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 4.3以下
     */
    public static boolean isNotificationEnabled() {

        boolean isNotification = true;
        Context context = BaseApplication.getAppInstance();

        NotificationManager nm = (NotificationManager)
                context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

        String pkg = context.getApplicationContext().getPackageName();

        try {
            Class NotificationManagerClass;
            // step1
            NotificationManagerClass = Class.forName(NotificationManager.class.getName());

            Method getServiceMethod = NotificationManagerClass.getMethod("getService");
            Logger.e("getServiceMethod: " + getServiceMethod);
            Object obj_inm = getServiceMethod.invoke(nm);
            Logger.e("obj_inm: " + obj_inm);

            // step2
            Class INotificationManagerClass;
            INotificationManagerClass = Class.forName("android.app.INotificationManager");

            // 多余步骤，看看nm里面有哪些方法，打出来看看
            Method[] list = INotificationManagerClass.getMethods();
            for (int i = 0; i < list.length; i++) {
                Logger.e(i + ": " + list[i].toString());
            }

            Method areNotificationsEnabledForPackage_Method = INotificationManagerClass.getMethod("areNotificationsEnabledForPackage", String.class);
            Logger.e("areNotificationsEnabledForPackage_Method: " + areNotificationsEnabledForPackage_Method);
            isNotification = (boolean) areNotificationsEnabledForPackage_Method.invoke(obj_inm, pkg);
            Logger.e("low4.3_invoke boo: " + isNotification);

        } catch (Exception e) {
            e.printStackTrace();
        }


//        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(DemoApp.getInstance());
//        boolean boo = notificationManagerCompat.areNotificationsEnabled();
        Logger.e("low4.3: " + isNotification);
        return isNotification;
    }
}
